package view;

import javax.swing.*;
import java.awt.Dimension;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

/**
 * 
 * This class creates the Chart which illustrates the amount of fat, carbs and protein. 
 * The chart is also based on the JFreeChart library. 
 * @author Roman Pusec, Dennis Goldfinger, Marko Bilic
 *
 */
public class DailyLogChart extends JFrame {

	private static final long serialVersionUID = 1L;
	
	/**
	 * Constructor which creates the chart. 
	 * @param fatAmount The amount of Fat.
	 * @param carbsAmount The amount of Carbs. 
	 * @param proteinAmount The amount of Protein. 
	 */
	public DailyLogChart(double fatAmount, double carbsAmount, double proteinAmount)
	{
		super("Chart");
        JFreeChart logChart = createLogChart(fatAmount, carbsAmount, proteinAmount);
        ChartPanel logChartPanel = new ChartPanel(logChart);
        logChartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(logChartPanel);
        pack();
        setVisible(true);
	}
	
	/**
	 * Creates the dataset of the chart. 
	 * @param fatAmount The amount of Fat.
	 * @param carbsAmount The amount of Carbs. 
	 * @param proteinAmount The amount of Protein. 
	 * @return The log chart. 
	 */
	private PieDataset createLogDataset(double fatAmount, double carbsAmount, double proteinAmount)
	{
		DefaultPieDataset dailyLogDataset = new DefaultPieDataset();
        dailyLogDataset.setValue("Fat", fatAmount);
        dailyLogDataset.setValue("Carbs", carbsAmount);
        dailyLogDataset.setValue("Protein", proteinAmount);
        return dailyLogDataset;
	}
	
	/**
	 * Creates the log chart. 
	 * @param fatAmount The amount of Fat.
	 * @param carbsAmount The amount of Carbs. 
	 * @param proteinAmount The amount of Protein. 
	 * @return The log chart. 
	 */
	private JFreeChart createLogChart(double fatAmount, double carbsAmount, double proteinAmount)
	{
		JFreeChart logChart = ChartFactory.createPieChart3D("Calories in terms of fat, carbs, and protein", createLogDataset(fatAmount, carbsAmount, proteinAmount), true, true, false);
		
		PiePlot3D plot = (PiePlot3D) logChart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return logChart;
	}
}
